import httpRequest from '@/utils/http-request'
import { ElMessage } from 'element-plus'

const AI_MATERIAL_API = 'http://localhost:5555'

/** 单个模板数据 */
export interface ICreateTaskParams {
  prompt: string
  chinese_prompt?: string
  emp_id: number | string
  emp_name: string
  email?: string;
}

/**
 * 生成一条ai素材
 * @returns
 */
export const createAITask = async (params: ICreateTaskParams) => {
  try {
    const { data } = await httpRequest.post(`${AI_MATERIAL_API}/tasks/create`, params)
    // const { data } = await httpRequest.post(`http://172.19.28.126:8081/tasks/create`, params)
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('生成AI图失败')
    return {}
  }
}

/**
 * 根据任务id获取任务详情
 */
export const getAITaskDetail = async (id: number | string) => {
  try {
    const { data } = await httpRequest.get(`${AI_MATERIAL_API}/tasks/get/${id}`)
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取图库失败')
    return {}
  }
}

/**
 * 根据分页获取任务列表
 */
export const getAITaskList = async ({ page, pageSize, empId }: any) => {
  try {
    const { data } = await httpRequest.get(`${AI_MATERIAL_API}/tasks/page`, {
      params: {
        page,
        pageSize,
        empId,
      },
    })
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取图库列表失败')
    return {}
  }
}

/**
 * 获取当天还剩余生成的次数
 */
export const getRemainCount = async (params: { empId: number }) => {
  try {
    const { data } = await httpRequest.get(`${AI_MATERIAL_API}/tasks/getCanUseCount`, {
      params: {
        empId: params.empId,
      },
    })
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取可用次数失败')
    return {}
  }
}
